<%@page import="service.OrderService"%>
<%@page import="entities.OrderEntity"%>
<%@page import="util.Util"%>
<%@page import="service.UserService"%>
<%@page import="service.CartService"%>
<%@page import="entities.UserEntity"%>
<%@page import="entities.CartEntity"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String userName = (String) session.getAttribute("userName");
	String orderNo = request.getParameter("orderNo");
	String randomNum = "";
	UserEntity user = null;
	OrderEntity order = new OrderEntity();
	List<CartEntity> cartList = new ArrayList<CartEntity>();
	List<OrderEntity> orderList = new ArrayList<OrderEntity>();
	String[][] payInfo = null;
	if (userName == null || orderNo == null) {
		response.sendRedirect("logReg.jsp?action=login");
	} else {
		//获得用户信息
		user = UserService.getUser(userName);

		if (orderNo.startsWith("T")) {
			randomNum = request.getParameter("randomNum");
			//获得订单数据
			orderList = OrderService.getOrder(userName, orderNo);
			if (orderList.size() != 0) {
				order = orderList.get(0);
			}
		} else {
			//获得订单数据
			orderList = OrderService.getOrder(userName, orderNo);

			if (orderList.size() != 0) {
				order = orderList.get(0);
				cartList = order.getCartList();
				//计算各个商家的应付款信息
				//Map<String,CartEntity> map  = new HashMap<String,CartEntity>();
				Map<String, List<CartEntity>> map = new HashMap<String, List<CartEntity>>();
				for (CartEntity cart : cartList) {
					// 订单分组
					if (map.containsKey(cart.getSellerName())) {// 已有该商家的名，当前订单项应该分配到的组
						map.get(cart.getSellerName()).add(cart);
					} else {// map中还没有该商家的Id,创建一个临时的List，将该商品添加到其中，并将List保存的map中
						List<CartEntity> tempList = new ArrayList<CartEntity>();
						tempList.add(cart);
						map.put(cart.getSellerName(), tempList);
					}
				}
				Set<String> keySet = map.keySet();

				//Map<String, String> map1 = new HashMap<String,String>();
				payInfo = new String[keySet.size()][3];
				int i = 0;
				for (Iterator it = keySet.iterator(); it.hasNext(); i++) {
					String key = (String) it.next();//key为商家名
					List<CartEntity> temp = map.get(key);
					double totalPrice = 0d;
					int totalCount = 0;
					for (CartEntity cart : temp) {
						totalCount += cart.getItemCount();
						totalPrice += cart.getGoodsPrice()
								* cart.getItemCount();
					}
					payInfo[i][0] = key;
					payInfo[i][1] = totalPrice + "";
					payInfo[i][2] = totalCount + "";
				}
			}
		}
	}
%>

<!DOCTYPE HTML>
<html>
<head>

<title>订单提交成功</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<link rel="stylesheet" type="text/css" href="css/cart.css">
<link rel="stylesheet" type="text/css" href="css/cartConfirm.css">

<style type="text/css">

/*订单信息*/
.orderInfo {
	border: 1px solid #ccc;
	padding: 20px;
}

.orderInfo div {
	margin-bottom: 10px;
}

.orderInfo span {
	color: red;
	font-size: 20px;
}

.orderInfo span.orderNo {
	color: red;
	font-weight: bold;
	font-size: 24px;
	margin-bottom: 10px;
}

.orderInfo span.infoSeller {
	color: #15ADFF;
	font-weight: bold;
	font-size: 16px;
}

.orderInfo span.infoPrice {
	color: red;
	font-size: 16px;
}

.orderInfo span.infoCount {
	color: #7CDAEF;
	font-weight: bold;
	font-size: 14px;
}

.orderInfo .go {
	margin-left: 300px;
	margin-top: 20px;
}

.orderInfo a {
	font-size: 14px;
	padding: 10px;
	margin-right: 10px;
	color: white;
	background-color: #15ADFF;
}

.orderInfo a:hover {
	background-color: #7CDAEF;
	color: red;
}
</style>

</head>

<body>
	<jsp:include page="header.jsp"></jsp:include>
	<div style="height: 30px;" id="userName"><%=userName%></div>
	<div class="cartConfirm">
		<div class="head">
			<span
				style="float: left;font-size:22px;margin-top:20px;color: green;">订单提交成功</span>
			<div class="step" style="float:right;margin:20px 0 0;">
				<img src="images/cart_step3_v1.jpg">
			</div>
		</div>

		<div class="orderInfo">
			<div>
				订 单 号 ：<span class="orderNo"><%=orderNo%></span>
			</div>
			<%if (orderNo.startsWith("T")) {%>
			<div>
				随机号码 ：<span class="orderNo"><%=randomNum%></span>
			</div>
			<%} %>
			<div style="float: left;width: 48%;">
				收 餐 人 ：<span><%=order.getToUser()%></span>
			</div>
			<div>
				送餐地址：<span><%=order.getToAddr()%></span>
			</div>
			<div style="float: left;width: 48%;">
				是否付款：<span><%=order.getState() == 1 ? "未付款" : "已付款"%></span>
			</div>
			<div>
				<%if (!orderNo.startsWith("T")) {
					if (order.getState() == 1 && cartList.get(0).getItemIsPaid() == 0) {
				%>
				应付款信息：其中: <span>
				 <%
				 	for (int i = 0; i < payInfo.length; i++) {
				 %>(<span class="infoSeller"><%=payInfo[i][0]%>:</span><span
									class="infoPrice">￥<%=payInfo[i][1]%> </span><span
									class="infoCount">数量:<%=payInfo[i][2]%></span>) <%
				 	}
				 %> </span>
				<%
					} else {
				%><span>&nbsp;</span>
				<%
					}
					}
				%>
			</div>
			<div class="go">
				<a id="sure" href="index.jsp">返回首页</a><a id="sure" href="seller.jsp">返回商家列表</a>
				<a id="sure" href="user/orderHistory.jsp?action=history">跟踪订单</a>
			</div>
		</div>
		<%	
			if ((!orderNo.startsWith("T"))&&cartList.size() != 0) {
		%>
		<div class="cart">
			<div class="info">订单详情</div>
		
			<div class="cart_products">
				<table width="100%">
					<tbody>
						<tr>
							<th width="170">产品</th>
							<th></th>
							<th width="120">数量</th>
							<th width="120">价格</th>
							<th width="120">总价</th>
							<th width="120">是否已付款</th>
						</tr>
						<%
							double totalPrice = 0d;
								for (CartEntity cart : cartList) {
									totalPrice += cart.getGoodsPrice() * cart.getItemCount();
						%>
						<tr class="cart_item" id="<%=cart.getGoodsId()%>">
							<td class="product_box" width="120"><div class="pic">
									<a href="goodsDetail.jsp?goodsId=<%=cart.getGoodsId()%>"
										target="_blank"> <img width="110" height="70"
										src="<%=cart.getGoodsImg()%>"> </a>
								</div>
							</td>
							<td class="name">
								<div style="width:259px;position: relative;">
									<a href="goodsDetail.jsp?goodsId=<%=cart.getGoodsId()%>"
										target="_blank"> <%=cart.getGoodsName()%> </a> <a
										class="sellerName"
										href="menu.jsp?sellerId=<%=cart.getSellerId()%>"
										target="_blank">【<%=cart.getSellerName()%>】</a>
									<div class="itemTime"><%=cart.getItemTime()%></div>
								</div></td>
							<td align="center" class="number_box"><%=cart.getItemCount()%></td>
							<td class="price_box">¥<span><%=cart.getGoodsPrice()%></span>
							</td>
							<td class="count_price_box">¥<span><%=cart.getGoodsPrice() * cart.getItemCount()%></span>
							</td>
							<td class="price_box"><span><%=(cart.getItemIsPaid() == 0 ? "否" : "是")%></span>
							</td>
						</tr>
						<%
							}
						%>
						<tr class="no_border">
							<td colspan="6" class="count">应付总额 ：<span class="totalPrice">
									¥<span><%=totalPrice%></span> </span></td>
						</tr>
					</tbody>
				</table>
			</div>
			<%
				}
			%>
		</div>

	</div>
	<jsp:include page="footer.jsp"></jsp:include>
</body>
</html>
